<!--
 * @Author: fisher
 * @Date: 2024-02-20 15:15:33
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-06-13 09:35:11
 * @FilePath: \allfront\src\modules\login\index.vue
 * @Description: 
-->
<template>
  <el-container class="login-container">
    <el-main class="login-main">
      <el-form v-if="!isRegister" :model="loginForm" ref="loginFormRef">
        <el-form-item label="用户名" prop="username" required>
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" required>
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit" @click="login">
            登录
          </el-button>
          <el-button type="primary" native-type="submit" @click="toRegister">
            注册
          </el-button>
        </el-form-item>
      </el-form>
      <div v-if="isRegister" class="registerForm">
        <div class="registerForm_item">
          <span class="registerForm_label">用户名:</span>
          <el-input v-model="registerForm.username"></el-input>
        </div>
        <div class="registerForm_item">
          <span class="registerForm_label">密码:</span>
          <el-input v-model="registerForm.password"></el-input>
        </div>
        <div class="registerForm_item">
          <span class="registerForm_label">邮箱:</span>
          <el-input v-model="registerForm.email"></el-input>
        </div>
        <el-button type="primary" @click="register"> 注册 </el-button>
      </div>
    </el-main>
  </el-container>
</template>
<script setup>
import { ref } from 'vue';
import { useAxios } from '@/hooks/useAxios';
import { useRouter } from 'vue-router';
const { post } = useAxios();
const router = useRouter();
const loginFormRef = ref(null);
const loginForm = ref({
  username: '',
  password: '',
});
const registerForm = ref({
  username: '',
  password: '',
  email: '',
});
const isRegister = ref(false);
const login = () => {
  // const form = loginFormRef.value;
  // form.validate(valid => {
  // if (valid) {
  const data = {
    username: loginForm.value.username,
    password: loginForm.value.password,
  };
  post('/Auth/login', data)
    .then(res => {
      localStorage.setItem('jwtToken', res.data.token);
      router.push({
        path: '/cloud',
      });
    })
    .catch(err => {
      throw new Error('Authentication failed', err);
    });
  // TODO: Insert your authentication logic here
  // } else {
  //   console.error('Validation failed');
  //   return false;
  // }
  // });
};
const toRegister = () => {
  isRegister.value = true;
};
const register = () => {
  const data = {
    username: registerForm.value.username,
    password: registerForm.value.password,
    email: registerForm.value.email,
  };
  console.log('register data == ', data);
  post('/Auth/register', data)
    .then(res => {
      console.log(res);
      if (res) {
        isRegister.value = false;
      }
    })
    .catch(err => {
      throw new Error('Authentication failed', err);
    });
};
</script>
<style>
.login-container {
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
}
.login-main {
  width: 100%;
  max-width: 400px;
}
.registerForm {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.registerForm_item {
  display: flex;
  gap: 10px;
}
.registerForm_label {
  width: 100px;
  text-align: right;
}
</style>
